/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */

package com.shishuo.cms.action;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang.Validate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.constant.FolderConstant;
import com.shishuo.cms.entity.Folder;
import com.shishuo.cms.entity.vo.FolderVo;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.entity.vo.PageVo;
import com.shishuo.cms.exception.FolderNotFoundException;
import com.shishuo.cms.exception.TemplateNotFoundException;

import freemarker.template.TemplateModelException;

/**
 * 首页
 * 
 * @author Herbert
 */
@Controller
public class IndexAction extends BaseAction {

	/**
	 * 首页
	 * 
	 * @param pageNum
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/index.htm", method = RequestMethod.GET)
	public String home(@RequestParam(value = "p", defaultValue = "1") long p,
			@RequestParam(value = "stock_code", defaultValue = "") String codeId,
			ModelMap modelMap) {
		try {
			modelMap.addAttribute("p", p);
			if(!codeId.equals("")){
				List<Folder> folderPathList;
				folderPathList = packageFolderByEname(codeId,modelMap);
				return themeService.getFolderTemplate(folderPathList);
			}
			return themeService.getDefaultTemplate();
		}catch (FolderNotFoundException e) {
			logger.fatal(e.getMessage());
			return themeService.getTemplatePath("404");
		} catch (TemplateNotFoundException e) {
			logger.fatal(e.getMessage());
			return themeService.getTemplatePath("404");
		}
	}
	@RequestMapping(value = "/left_tool_bar.htm", method = RequestMethod.GET)
	public String sideBar(@RequestParam(value = "folderId", defaultValue = "1") long folderId,ModelMap modelMap) {
		try {
			List<FolderVo> nextList=folderService.getAllFolderList(folderId,
					FolderConstant.Status.display);
			//获取上级
			if (null!=nextList) {
				if (nextList != null&&nextList.size()>0) {
					FolderVo firstFolder=folderService.getFolderById(nextList.get(0).getFirstFolderId());
					FolderVo secondLevelFolder=folderService.getFolderById(nextList.get(0).getSecondFolderId());
					modelMap.put("firstFolder", DEFAULT_WRAPPER.wrap(firstFolder));
					modelMap.put("secondLevelFolder", DEFAULT_WRAPPER.wrap(secondLevelFolder));
				}
			}
			modelMap.put("tag_folder_list_next", DEFAULT_WRAPPER.wrap(nextList));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return themeService.getTemplatePath("left_tool_bar");
	}
	@ResponseBody
    @RequestMapping(value = "/left_tool_bar.json")
    public String sideBarForJson(@RequestParam(value = "folderId", defaultValue = "1") long folderId,
            @RequestParam(value = "p", defaultValue = "1") int p,
            ModelMap modelMap) {
        JSONObject json = new JSONObject();
        try {
            json.put("result",true);
            /*PageVo<FolderVo> nextList=folderService.getFolderPageByFoderFatherId(folderId,
                   p, FolderConstant.Status.display);*/
            List<FolderVo> nextList=folderService.getFolderListByFatherId(folderId,FolderConstant.Status.display);
            //获取上级
            if (null!=nextList) {
                if (nextList != null&&nextList.size()>0) {
                    FolderVo firstFolder=folderService.getFolderById(nextList.get(0).getFirstFolderId());
                    FolderVo secondLevelFolder=folderService.getFolderById(nextList.get(0).getSecondFolderId());
                    json.put("firstFolder", firstFolder);
                    json.put("secondLevelFolder", secondLevelFolder);
                }
            }
            json.put("tag_folder_list_next", nextList);
        } catch (Exception e) {
            json.put("result",false);
            json.put("msg", e.getMessage());
        } 
        return json.toString();
    }
	
	@ResponseBody
	@RequestMapping(value = "/search.json", method = RequestMethod.POST)
	public JsonVo<String> search(@RequestParam(value = "codeId", defaultValue = "000001") String codeId,
			ModelMap modelMap) {
		JsonVo<String> json = new JsonVo<String>();
		try {
			List<Folder> folderPathList = packageFolderByEname(codeId,modelMap);
			json.setResult(true);
			if(folderPathList.size()<=2){
				json.setResult(false);
			}
			String pathString="/folder";
			for (int i = 0; i < folderPathList.size(); i++) {
				pathString+="/"+folderPathList.get(i).getEname();
			}
			pathString+="/index.htm";
			json.setT(pathString);
		} catch (FolderNotFoundException e) {
			json.setResult(false);
			json.setMsg(e.getMessage());
		} 
		return json;
	}
	/**
	 * 404
	 * 
	 * @return
	 */
	@RequestMapping(value = "/404.htm", method = RequestMethod.GET)
	public String pageNotFound() {
		return themeService.getTemplatePath("404");
	}

	/**
	 * 500
	 * 
	 * @return
	 */
	@RequestMapping(value = "/500.htm", method = RequestMethod.GET)
	public String error() {
		return themeService.getTemplatePath("500");
	}
}
